<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="mfuny.ggongal.admin.http.HttpUser" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="org.json.simple.*" %>
<%@ page import="mfuny.ggongal.admin.mygift.MyGiftService" %>
<%@ page import="mfuny.ggongal.admin.json.JsonCreater" %>
<%@ page import="mfuny.ggongal.admin.debug.Debug" %>
<%@ page import="mfuny.ggongal.admin.http.HttpUserDecoder" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%
request.setCharacterEncoding( "utf-8" );
response.setContentType( "text/html; charset=utf-8" );
response.addHeader("Access-Control-Allow-Origin", "*");

JsonCreater jc = new JsonCreater();
HttpUser user = new HttpUser();
HttpUserDecoder decoder = new HttpUserDecoder();
MyGiftService myGift = new MyGiftService();

String ggongal = request.getParameter("ggongal");
if( ggongal != null ) {
	try{
		String result = decoder.receiveDecode(ggongal);
		out.print(result);
	}catch(Exception e) {
		Debug.toDebugFile(e, "ggongal");
	}
	return;
}

String tag = request.getParameter( "tag" );

//회원가입.
//if( tag.equals( "user_insert2" ) ) {
//	try{
		//HashMap<String, String> map = new HashMap<String, String>();
//		map.put( "id", request.getParameter( "id" ) );
//		map.put( "pw", request.getParameter( "pw" ) );
//		map.put( "birth_y", request.getParameter( "birth_y" ) );
//		map.put( "birth_m", request.getParameter( "birth_m" ) );
//		map.put( "birth_d", request.getParameter( "birth_d" ) );
//		map.put( "email", request.getParameter( "email" ) );
//		map.put( "pnum", request.getParameter( "pnum" ) );
//		map.put( "sex", request.getParameter( "sex" ) );
//		map.put( "marry", request.getParameter( "marry" ) );
//		map.put( "job", request.getParameter( "job" ) );
//		map.put( "area", request.getParameter( "area" ) );
//		map.put( "pid", request.getParameter( "pid" ) );
//		map.put( "pname", request.getParameter( "pname" ) );
//		map.put( "pserial", request.getParameter( "pserial" ) );
//		map.put( "recommend", request.getParameter( "recommend" ) );
//		int result = user.user_insert( map );
//		JSONObject json = new JSONObject();
//		json.put( "responseCode", result );
//		out.print( json.toJSONString( ) );
	//}catch(Exception e){
//		Debug.toDebugFile(e, tag);
//	}
	
//} 

//회원가입2, 축하 꽁 지급량 db에서 가져와 처리.
if( tag.equals( "user_insert" ) ) {
	try{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put( "id", request.getParameter( "id" ) );
		map.put( "pw", request.getParameter( "pw" ) );
		map.put( "birth_y", request.getParameter( "birth_y" ) );
		map.put( "birth_m", request.getParameter( "birth_m" ) );
		map.put( "birth_d", request.getParameter( "birth_d" ) );
		map.put( "email", request.getParameter( "email" ) );
		map.put( "pnum", request.getParameter( "pnum" ) );
		map.put( "sex", request.getParameter( "sex" ) );
		map.put( "marry", request.getParameter( "marry" ) );
		map.put( "job", request.getParameter( "job" ) );
		map.put( "area", request.getParameter( "area" ) );
		map.put( "pid", request.getParameter( "pid" ) );
		map.put( "pname", request.getParameter( "pname" ) );
		map.put( "pserial", request.getParameter( "pserial" ) );
		map.put( "recommend", request.getParameter( "recommend" ) );
		
		JSONObject json = user.user_insert2(map);
		out.print( json.toJSONString( ) );
		
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
	
} 


//로그인 체크.
else if ( tag.equals( "user_login" )) {
	String id = request.getParameter( "id" );
	try{
		String pw = request.getParameter( "pw" );
		String pnum = request.getParameter("pnum");
		String pname = request.getParameter("pname");
		String pserial = request.getParameter("pserial");
		JSONObject json = user.user_login( id, pw, pnum, pname, pserial );
		out.print( json.toJSONString( ) );
	}catch(Exception e){
		Debug.toDebugFile(e, tag + " / " + id);
	}
}

//인증번호 요청.
else if(tag.equals("user_requestPin")) {
	JSONObject json = new JSONObject();
	try{
		String pnum = request.getParameter("pnum");
		json.put("responseCode", user.requestPin(pnum));
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
		json.put("responseCode", -1);
		out.print(json.toJSONString());
	}
}

//인증번호 응답.
else if(tag.equals("user_responsePin")) {
	JSONObject json = new JSONObject();
	try{
		String pnum = request.getParameter("pnum");
		String pin = request.getParameter("pin");
		json.put("responseCode", user.responsePin(pnum, pin));
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
		json.put("responseCode", -1);
		out.print(json.toJSONString());
	}
}

//로그인 시 인증번호 확인.
else if(tag.equals("user_responsePinForLogin")) {
	JSONObject json = null;
	try{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("pin", request.getParameter("pin"));
		params.put("id", request.getParameter("id"));
		params.put("pnum", request.getParameter("pnum"));
		params.put("pname", request.getParameter("pname"));
		params.put("pserial", request.getParameter("pserial"));
		params.put("pid", request.getParameter("pid"));
		json = user.responsePinForLogin(params);
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
		json = new JSONObject();
		json.put("responseCode", -1);
		json.put("error", e);
		out.print(json.toJSONString());
	}
}

//로그인 후 스페셜게임 검사건이 있는 체크.
else if(tag.equals("user_checkSpecial")) {
	try{
		String id = request.getParameter("id");
		JSONObject json = user.user_checkSpecial(id);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag + " , id: " + request.getParameter("id"));
	}
}

//해당 유저의 출석 현황.
else if(tag.equals("user_getLogin_level")) {
	try{
		String id = request.getParameter("id");
		JSONObject json = user.user_getLogin_level(id);
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
	}
}

//아이디 존재여부 체크.
else if ( tag.equals( "user_idCheck" )) {
	try{
		String id = request.getParameter( "id" );
		int result = user.user_idCheck( id );
		JSONObject json = new JSONObject();
		json.put( "responseCode", result );
		out.print( json.toJSONString( ) );
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//이메일 존재여부 체크.
else if(tag.equals("user_emailCheck")) {
	try{
		String email = request.getParameter("email");
		JSONObject json = user.user_emailCheck(email);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//아이디 찾기.
else if(tag.equals("user_idFind")) {
	try{
		String pnum = request.getParameter("pnum");
		String email = request.getParameter("email");
		JSONObject json = user.idFind(pnum, email);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//비번찾기.
else if(tag.equals("user_pwFind")) {
	try{
		String id = request.getParameter("id");
		String pnum = request.getParameter("pnum");
		String email = request.getParameter("email");
		JSONObject json = user.pwFind(id, pnum, email);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//비번체크.
else if(tag.equals("user_pwCheck")) {
	try{
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		JSONObject json = user.user_pwCheck(id, pw);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//비번변경.
else if(tag.equals("user_pwUpdate")) {
	try{
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		JSONObject json = user.user_pwUpdate(id, pw);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//개인정보변경.
else if(tag.equals("user_infoUpdate")) {
	try{
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", request.getParameter("id"));
		map.put("area", request.getParameter("area"));
		map.put("sex", request.getParameter("sex"));
		map.put("marry", request.getParameter("marry"));
		map.put("job", request.getParameter("job"));
		map.put("birth_y", request.getParameter("birth_y"));
		map.put("birth_m", request.getParameter("birth_m"));
		map.put("birth_d", request.getParameter("birth_d"));
		map.put("recommend", request.getParameter("recommend"));
		
		System.out.println("parameter : " + map);	//111.
		
		JSONObject json = user.user_infoUpdate(map);
		
		System.out.println(json.toJSONString());		//111.
		
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
		JSONObject json = new JSONObject();
		json.put("responseCode", -1);
		out.print(json.toJSONString());
	}
}

//우편번호 검색.
else if(tag.equals("user_dongSearch")) {
	try{
		String dong = request.getParameter("dong");
		JSONObject json = user.user_dongSearch(dong);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//배송지 입력.
else if(tag.equals("user_setAddress")) {
	try{
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("id", request.getParameter("id"));
		params.put("zip", request.getParameter("zip"));
		params.put("address_1", request.getParameter("address_1"));
		params.put("address_2", request.getParameter("address_2"));
		JSONObject json = user.user_setAddress(params);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//꽁 가감 처리.
//else if(tag.equals("user_addGgong")) {
//	try{
//		HashMap<String, Object> params = new HashMap<String, Object>();
//		params.put("id", request.getParameter("id"));
//		params.put("ggong", Integer.parseInt(request.getParameter("ggong")));
//		params.put("reason", request.getParameter("reason") == null ? null : request.getParameter("reason"));
//		JSONObject json = user.user_addGgong(params);
//		out.print(json.toJSONString());
//	}catch(Exception e) {
//		Debug.toDebugFile(e, tag);
//	}
//}

//꽁 가감 처리.
else if(tag.equals("user_addGgong2")) {
	try{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("id", request.getParameter("id"));
		params.put("ggong", Integer.parseInt(request.getParameter("ggong")));
		params.put("nowGgong", Integer.parseInt(request.getParameter("nowGgong")));
		params.put("reason", request.getParameter("reason") == null ? null : request.getParameter("reason"));
		JSONObject json = user.user_addGgong2(params);
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
	}
}

//꽁 내역 가져오기.
else if(tag.equals("user_getMyGgongList")) {
	try{
		String id = request.getParameter("id");
		int start = Integer.parseInt(request.getParameter("start"));
		String type = request.getParameter("type");
		JSONObject json = user.user_getMyGgongList(id, start, type);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
		JSONObject json = new JSONObject();
		json.put("responseCode", -1);
		out.print(json.toJSONString());
	}
}

//꽁 랭킹(글로벌) 가져오기.
else if(tag.equals("user_getGgongRank")) {
	try{
		int start = Integer.parseInt(request.getParameter("start"));
		String id = request.getParameter("id");
		JSONObject json = user.user_getGgongRank(id, start);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
		JSONObject json = new JSONObject();
		json.put("responseCode", -1);
		out.print(json.toJSONString());
	}
}

//경품 랭킹(글로벌) 가져오기.
else if( tag.equals("user_getGiftRank")) {
	JSONObject json = null;
	try {
		int start = Integer.parseInt(request.getParameter("start"));
		String id = request.getParameter("id");
		json = user.user_getGiftRank(id, start);
		out.print(json.toJSONString());
	} catch(Exception e) {
		Debug.toDebugFile(e, tag);
		json = new JSONObject();
		json.put("responseCode", -1);
		out.print(json.toJSONString());
	}
}

//유저 연락처에서 친구 리스트와 꽁랭킹 가져오기.
else if(tag.equals("user_getMyFriends")) {
	try{
		String pnums = request.getParameter("pnums");
		JSONObject json = user.user_getMyFriends(pnums);
		out.print(json.toJSONString());
		
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//유저 연락처에서 친구 리스트와 경품랭킹 가져오기.
else if(tag.equals("user_getMyFriendsGiftRank")) {
	try{
		String pnums = request.getParameter("pnums");
		JSONObject json = user.user_getMyFriendsGiftRank(pnums);
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
	}
}

//내친구 꽁랭킹 가져오기.
//미사용.
else if(tag.equals("user_getGgongRankByFriends")) {
	try{
		String friendsId = request.getParameter("friendsId");
		JSONObject json = user.user_getGgongRankByFriends(friendsId);
		out.print(json.toJSONString());
		
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//특정 번호의 내경품 정보.
else if(tag.equals("myGift_getMyGiftByNo")) {
	try{
		int mygift_no = Integer.parseInt(request.getParameter("mygift_no"));
		JSONObject json = myGift.getMyGiftByNo(mygift_no);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//경품 받기로 했을경우 추가 입력.
else if(tag.equals("myGift_setMyGift2")) {
	try{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("id", request.getParameter("id"));
		params.put("zip", request.getParameter("zip"));
		params.put("address_1", request.getParameter("address_1"));
		params.put("address_2", request.getParameter("address_2"));
		params.put("recipient", request.getParameter("recipient"));
		params.put("depositname", request.getParameter("depositname"));
		params.put("mygift_no", Integer.parseInt(request.getParameter("mygift_no")));
		JSONObject json = myGift.setMyGift2(params);
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
	}
}

//나의 경품당첨 리스트 가져오기.
else if(tag.equals("user_getMyGifts")) {
	try{
		String id = request.getParameter("id");
		int start = Integer.parseInt(request.getParameter("start"));
		int type = Integer.parseInt(request.getParameter("type"));
		JSONObject json = user.user_getMyGifts(id, start, type);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//특정 광고(스페셜게임)의 당첨자 리스트 가져오기.
else if(tag.equals("user_getWinnerList")) {
	try{
		int ad_no = Integer.parseInt(request.getParameter("ad_no"));
		String days = request.getParameter("days");
		JSONObject json = user.user_getWinnerList(ad_no, days);
		out.print(json.toJSONString());
	}catch(Exception e) {
		Debug.toDebugFile(e, tag);
	}
}

//현재 꽁 가져오기.
else if(tag.equals("user_getGgong")) {
	try{
		String id = request.getParameter("id");
		JSONObject json = user.user_getGgong(id);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//1:1 문의 등록.
else if(tag.equals("user_regAsk")) {
	try{
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("id", request.getParameter("id"));
		params.put("contents", request.getParameter("contents"));
		params.put("type", request.getParameter("type"));
		params.put("subject", request.getParameter("subject"));
		JSONObject json = user.user_regAsk(params);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//나의 1:1 문의 내역.
else if(tag.equals("user_getMyAskList")) {
	try{
		String id = request.getParameter("id");
		int start = Integer.parseInt(request.getParameter("start"));
		JSONObject json = user.user_getMyAskList(id, start);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//사진 업로드.
else if(tag.equals("user_setMyPic")) {
	try{
		String id = request.getParameter("id");
		String mypic = request.getParameter("mypic");
		JSONObject json = user.user_setMyPic(id, mypic);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}

//탈퇴처리.
else if(tag.equals("user_out")) {
	try{
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("id", request.getParameter("id"));
		params.put("outreason", request.getParameter("outreason"));
		JSONObject json = user.user_out(params);
		out.print(json.toJSONString());
	}catch(Exception e){
		Debug.toDebugFile(e, tag);
	}
}
%>